﻿using System.Linq;
using DNT.RavenQA.Core.Extensions;
using DNT.RavenQA.Core.Models;
using DNT.RavenQA.Web.Infrastructure.Indexing;
using Raven.Client;
using Raven.Client.Linq;

namespace DNT.RavenQA.Web.Infrastructure.QueryModel
{
    public class FeaturedTagsQuery : IQuery<QuestionsTagListIndex.ReduceResult>
    {
        public FeaturedTagsQuery(Question question)
        {
            question.EnsureIsNotNull("question");
            _question = question;
        }

        private readonly Question _question;

        public IQueryable<QuestionsTagListIndex.ReduceResult> ExecuteWithStats(IDocumentSession session,
                                                                               out RavenQueryStatistics stats)
        {
            return session.Query<QuestionsTagListIndex.ReduceResult, QuestionsTagListIndex>()
                .Statistics(out stats)
                .Where(t => t.Name.In(_question.Tags));
        }

        public IQueryable<QuestionsTagListIndex.ReduceResult> Execute(IDocumentSession session)
        {
            return session.Query<QuestionsTagListIndex.ReduceResult, QuestionsTagListIndex>()
                .Where(t => t.Name.In(_question.Tags));
        }
    }
}